<?php

function tbnd_login_google_authenticate() {
    module_load_include('php', 'tbnd_login', '/assets/lib/lightopenid/openid');
    if (isset($_GET['openid_ext1_value_email'])) {
        $uid = tbnd_login_google_login_validate($_GET);
        if ($uid != null)
            $uid = 'Google';
        print theme('tbnd_login_oauth_popup', array('user_oauth' => $uid));
    } else {
        $openid = new LightOpenID(BASE_URL);
        $openid->identity = 'https://www.google.com/accounts/o8/id';
        //setting call back url
        $openid->returnUrl = BASE_URL . 'google/auth';
        //finding open id end point from google
        $endpoint = $openid->discover('https://www.google.com/accounts/o8/id');
        $fields =
                '?openid.ns=' . urlencode('http://specs.openid.net/auth/2.0') .
                '&openid.return_to=' . urlencode($openid->returnUrl) .
                '&openid.claimed_id=' . urlencode('http://specs.openid.net/auth/2.0/identifier_select') .
                '&openid.identity=' . urlencode('http://specs.openid.net/auth/2.0/identifier_select') .
                '&openid.mode=' . urlencode('checkid_setup') .
                '&openid.ns.ax=' . urlencode('http://openid.net/srv/ax/1.0') .
                '&openid.ax.mode=' . urlencode('fetch_request') .
                '&openid.ax.required=' . urlencode('email,firstname,lastname,dob,gender,fullname') .
                '&openid.ax.type.firstname=' . urlencode('http://axschema.org/namePerson/first') .
                '&openid.ax.type.lastname=' . urlencode('http://axschema.org/namePerson/last') .
                '&openid.ax.type.fullname=' . urlencode('http://axschema.org/namePerson') .
                '&openid.ax.type.dob=' . urlencode('http://axschema.org/birthDate') .
                '&openid.ax.type.gender=' . urlencode('http://axschema.org/person/gender') .
                '&openid.ax.type.email=' . urlencode('http://axschema.org/contact/email');
        header('Location: ' . $endpoint . $fields);
    }
}

/**
 * Login bằng facebook
 */
function tbnd_login_facebook_authenticate() {
    module_load_include('php', 'tbnd_login', '/assets/lib/facebook/facebook');
    $facebook = new Facebook(array(
                'appId' => OAUTH_FACEBOOK_APP_ID,
                'secret' => OAUTH_FACEBOOK_CONSUMER_SECRET,
            ));
    $fb_user = $facebook->getUser(); 
    
    if ($fb_user || $fb_user != 0) {  
		try {
            $user_profile = $facebook->api('/me');      
            $uid = tbnd_login_facebook_login_validate($user_profile);
            if ($uid != null)
                $uid = 'Facebook';
            print theme('tbnd_login_oauth_popup', array('user_oauth' => $user_profile));
			$facebook->destroySession();	
        } catch (FacebookApiException $e) {
            $fb_user = null;
			//$facebook->destroySession();	
        }
    } else {        
        
       $loginUrl = $facebook->getLoginUrl(
                                        array(
                                            'display'   => 'popup',
                                            'scope' => 'email,read_stream, publish_stream,user_birthday, user_location, user_work_history, user_hometown, user_photos'
                                          // 'next' => 'index.php',
                                           // 'cancel_url' => 'index.php'
                                            )
                                );    
        //$loginUrl = $facebook->getLoginUrl(array('scope' => 'email,read_stream, publish_stream,user_birthday, user_location, user_work_history, user_hometown, user_photos'));
        header('Location: ' . $loginUrl);
    }
}
?>